package com.example.permissionservice.repository;

import com.example.permissionservice.entity.UserRoleEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Repository
public interface UserRoleRepository extends JpaRepository<UserRoleEntity, Long> {
    Optional<UserRoleEntity> findByUserId(Long userId);
    boolean existsByUserId(Long userId);

    @Transactional
    @Modifying
    @Query("UPDATE UserRoleEntity ur SET ur.roleId = :roleId WHERE ur.userId = :userId")
    int updateRoleByUserId(@Param("userId") Long userId, @Param("roleId") Integer roleId);
}